// DlgRequestNA.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgRequestNA.h"
#include "DlgPreviewHTML.h"
#include "SendMessage.h"
#include "DlgPickList.h"

// CDlgRequestNA dialog

IMPLEMENT_DYNAMIC(CDlgRequestNA, CDialog)

CDlgRequestNA::CDlgRequestNA(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgRequestNA::IDD, pParent)
	, m_bEmailUser(TRUE)
	, m_dServiceFee(0)
	, m_dCopyrightFee(0)
	, m_dMiscFee(0)
	, m_szPMID(_T(""))
	, m_szOCLC(_T(""))
	, m_szNLMID(_T(""))
	, m_szInfo(_T(""))
	, m_szUserNotes(_T(""))
{
	m_nStaffID = 0;
	m_nItemID = 0;
	m_nUserID = 0;
	m_nLibraryID = 0;
	m_nStatus = -1;
	m_nReason = 0;
	m_nILL = 0;
}

CDlgRequestNA::~CDlgRequestNA()
{
}

void CDlgRequestNA::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_PICK, m_ctrlReason);
	DDX_Check(pDX, IDC_CHECK_SEND_EMAIL, m_bEmailUser);
	DDX_Text(pDX, IDC_EDIT_SERVICE_FEE, m_dServiceFee);
	DDX_Text(pDX, IDC_EDIT_COPYRIGHT_FEE, m_dCopyrightFee);
	DDX_Text(pDX, IDC_EDIT_MISC_FEE, m_dMiscFee);
	DDX_Text(pDX, IDC_EDIT_PMID, m_szPMID);
	DDX_Text(pDX, IDC_EDIT_OCLC, m_szOCLC);
	DDX_Text(pDX, IDC_EDIT_NLMID, m_szNLMID);
	DDX_Text(pDX, IDC_EDIT_INFO, m_szInfo);
	DDX_Text(pDX, IDC_EDIT_USER_NOTES, m_szUserNotes);
}


BEGIN_MESSAGE_MAP(CDlgRequestNA, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_ILL, &CDlgRequestNA::OnBnClickedButtonIll)
	ON_LBN_SELCHANGE(IDC_LIST_PICK, &CDlgRequestNA::OnLbnSelchangeListPick)
END_MESSAGE_MAP()


// CDlgRequestNA message handlers

BOOL CDlgRequestNA::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_nItemID != 0 );

	// disable combine buttons
	if( !appConfig.m_Options.bDisableCombineNASendReturn )
	{
		GetDlgItem( IDC_BUTTON_ILL )->ShowWindow( SW_HIDE );
	}

	// get if sticky
	if( appConfig.m_General.bUseStickyNAReason )
		m_nReason = AfxGetApp()->GetProfileInt( _T("Settings"), _T("StickyNAReason"), m_nReason );

	// get hold desricptions
	CString szTemp;
	INT nSel = 0;
	INT nCurSel = 0;

	appConfig.m_SQL.GetDescriptionStatuss( &m_aStatus );
	//m_ctrlStatus
	for( INT nIndex = 0; nIndex < m_aStatus.GetCount(); nIndex++ )
	{
		// check for hold
		if( m_aStatus[nIndex].nID >= STATUS_OFFSET_NA && m_aStatus[nIndex].nID <= STATUS_OFFSET_NA + 99 )
		{
			// format text
			szTemp.Format( _T("%.3d   %s"), m_aStatus[nIndex].nID, m_aStatus[nIndex].szExplanation );
			// add to array
			nSel = m_ctrlReason.AddString( szTemp );
			m_aStatusID.Add( m_aStatus[nIndex].nID );
			// set selection
			if( m_aStatus[nIndex].nID == m_nReason )
				nCurSel = nSel;
		}
	}
	m_ctrlReason.SetCurSel( nCurSel );

	// set title
	CString szText;
	szText.Format( _T("Request Not Availalbe: %.8ld"), m_nItemID );
	SetWindowText( szText );

	// get request
	REQUEST_SET Request;
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );
	// get variables
	m_szRequest = Request.m_request;
	m_nUserID = Request.m_user_id;
	m_nLibraryID = Request.m_library_id;
	m_szDateIn = Request.m_date_in.Format( appConfig.m_General.szDateFormat );
	m_dServiceFee = Request.m_fee_service;
	m_dCopyrightFee = Request.m_fee_copyright;
	m_dMiscFee = Request.m_fee_misc;
	m_szUID = Request.m_uid;

	// ILL enabled
	if( !appConfig.m_Options.bDisableILL )
	{
		m_nILL = Request.m_ill_id;
		m_szILL = appConfig.m_SQL.GetDescription( CMySQLEx::ill, Request.m_ill_id );
	}

	// get user
	USER_SET User;
	appConfig.m_SQL.GetUserSet( &User, m_nUserID );
	// get variables
	m_szTo = User.m_email;

	// disable ILL
	if( appConfig.m_Options.bDisableILL )
	{
		GetDlgItem( IDC_BUTTON_ILL )->ShowWindow( SW_HIDE );
		// set info
		m_szInfo.Format( _T("Name: %s\r\nIdentifier: %s\r\nDate In: %s\r\n\r\nRequest:\r\n%s"), User.m_name, m_szUID, m_szDateIn, m_szRequest );
	}
	else
	{
		// set info
		m_szInfo.Format( _T("Name: %s\r\nIdentifier: %s\r\nILL: %s\r\n\r\nRequest:\r\n%s"), User.m_name, m_szUID, m_szILL, m_szRequest );
	}

	// show finalcials
	if( appConfig.m_Options.bDisableFinacial )
	{
		GetDlgItem( IDC_STATIC_SERVICE_FEE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_SERVICE_FEE )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_MISC_FEE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_MISC_FEE )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_COPYRIGHT_FEE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_COPYRIGHT_FEE )->ShowWindow( SW_HIDE );
	}

	// disable ILL
	if( m_nILL < 1 )
		GetDlgItem( IDC_BUTTON_ILL )->EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );

	// update buttons
	UpdateButtons();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgRequestNA::OnOK()
{
	// update data
	UpdateData();

	// check selection
	INT nSelection = m_ctrlReason.GetCurSel();
	if( nSelection == -1 )
	{
		AfxMessageBox( _T("Please select reason request not available.") );
		return;
	}
	
	// get reason ID
	m_nReason = m_aStatusID.GetAt( nSelection );

	// get status description
	STATUS_LOOKUP Status;
	appConfig.m_SQL.GetDescriptionStatus( &Status, m_nReason );
	m_szReason = Status.szExplanation;

	// send direct to ILL
	if( m_nILL && Status.nSendILL > 0 && !appConfig.m_Options.bDisableCombineNASendReturn )
	{
		OnBnClickedButtonIll();
		return;
	}

	// show popup
	if( !Status.szInformation.IsEmpty() )
	{
		if( AfxMessageBox( Status.szInformation, MB_YESNO ) == IDNO )
			return;
	}

	// check for ILL
	if( m_nILL > 0 && !appConfig.m_Options.bDisableILL )
	{
		// check if ILL available for reason
		if( m_aStatus.GetAt( m_nReason ).nSendILL > 0 )
		{
			if( AfxMessageBox( _T("The user is requesting ILL, did you want to return this to the user?"), MB_YESNO ) == IDNO )
				return;
		}
	}

	// get request
	REQUEST_SET Request;
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );

	// send message
	if( m_bEmailUser )
	{
		// get library info
		LIBRARY_LOOKUP Library;
		appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nLibraryID );
		m_szFrom = Library.szEmail;
		m_szName = Library.szName;
		m_szSubject = Library.szSubject;

		// read message
		if( !Status.szMessageFile.IsEmpty() )
			m_szMessage = appConfig.ReadFile( Status.szMessageFile );
		else if( Request.m_external_id > 0 && !Status.szMessageFileExt.IsEmpty() )
			m_szMessage = appConfig.ReadFile( Status.szMessageFileExt );
		else if( Status.nComplete )
			m_szMessage = appConfig.ReadFile( appConfig.m_Email.szSendMessageFileNA1 );
		else
			m_szMessage = appConfig.ReadFile( appConfig.m_Email.szSendMessageFileNA );

		// get external request user id
		if( Request.m_external_id > 0 )
		{
			USER_SET User;
			if( appConfig.m_SQL.GetExternalUserID( &User, m_szTo ))
			{
				CString szTemp;
				szTemp.Format( _T("%ld"), User.m_id );
				m_szMessage.Replace( _T("(USERID)"), szTemp );
				m_szMessage.Replace( _T("(USERPID)"), User.m_pwd );
			}
		}

		// format message
		appConfig.FormatMacros( &m_szMessage, m_nItemID, -1, m_nReason );
		// format subject macros
		appConfig.FormatMacros( &m_szSubject, m_nItemID, -1, m_nReason );

		// show preivew
		CDlgPreviewHTML dlgPreview;
		dlgPreview.m_bSend = TRUE;
		dlgPreview.m_szHTML = m_szMessage;
		dlgPreview.m_szSubject = m_szSubject;

		// show message
		if( dlgPreview.DoModal() != IDOK )
			return;

		// get new message
		m_szMessage = dlgPreview.m_szHTML;
		m_szSubject = dlgPreview.m_szSubject;

		// send message
		CSendMessage send;
		if( !send.SendEmail( m_szName, m_szFrom, m_szTo, m_szSubject, m_szMessage ))
			return;
	}

	// set variables
	Request.m_staff_id = m_nStaffID;	
	Request.m_date_out = COleDateTime::GetCurrentTime();
	Request.m_touched = TRUE;
	Request.m_fee_service = m_dServiceFee;
	Request.m_fee_copyright = m_dCopyrightFee;
	Request.m_fee_misc = m_dMiscFee;
	Request.m_notes = m_szUserNotes;
	Request.m_retain = 0;
	// get unique ID
	Request.m_uid = GetIDString();
	// set status
	if( Status.nComplete )
		Request.m_status_id = STATUS_ID_COMPLETE;
	else
		Request.m_status_id = m_nReason;

	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// save status for return
	m_nStatus = Request.m_status_id;

	// set if sticky
	if( appConfig.m_General.bUseStickyNAReason )
		AfxGetApp()->WriteProfileInt( _T("Settings"), _T("StickyNAReason"), m_nReason );

	CDialog::OnOK();
}

void CDlgRequestNA::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}

void CDlgRequestNA::OnBnClickedButtonIll()
{
	// update data
	UpdateData();

	// check selection
	INT nSelection = m_ctrlReason.GetCurSel();
	if( nSelection == -1 )
	{
		AfxMessageBox( _T("Please select reason request not available.") );
		return;
	}

	// get reason ID
	m_nReason = m_aStatusID.GetAt( nSelection );

	CStringArray szaLibrary;
	LONG nLibraryID = 0;
	LIBRARY_LOOKUP Library;

	// check for default ILL
	STAFF_SET Staff;
	appConfig.m_SQL.GetStaffSet( &Staff, m_nStaffID );
	nLibraryID = Staff.m_default_ill_library;
	if( nLibraryID < 0 )
	{
		// get library descriptions
		appConfig.m_SQL.GetDescriptions( CMySQLEx::library, &szaLibrary, CMySQLEx::list_ILL );

		// show pick list
		CDlgPickList PickListDlg;
		PickListDlg.m_pszaStrings = &szaLibrary;
		if( PickListDlg.DoModal() == IDOK )
		{
			// set user filter				
			nLibraryID = _tstol( PickListDlg.m_szString.Left( PickListDlg.m_szString.Find( _T(" ") )));
		}
		else
		{
			return;
		}
	}

	// send message
	if( m_bEmailUser )
	{
		CString szNAMessage;

		// get library info
		LIBRARY_LOOKUP Library;
		appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nLibraryID );
		m_szFrom = Library.szEmail;
		m_szName = Library.szName;
		m_szSubject = Library.szSubject;

		// read message
		m_szMessage = appConfig.ReadFile( appConfig.m_Email.szMoveILLMessageFile );

		// format message
		appConfig.FormatMacros( &m_szMessage, m_nItemID );
		// format subject macros
		appConfig.FormatMacros( &m_szSubject, m_nItemID, -1, m_nReason );

		// show preivew
		CDlgPreviewHTML dlgPreview;
		dlgPreview.m_bSend = TRUE;
		dlgPreview.m_szHTML = m_szMessage;
		dlgPreview.m_szSubject = m_szSubject;

		// show message
		if( dlgPreview.DoModal() != IDOK )
			return;

		// get new message
		m_szMessage = dlgPreview.m_szHTML;
		m_szSubject = dlgPreview.m_szSubject;

		// send message
		CSendMessage send;
		if( !send.SendEmail( m_szName, m_szFrom, m_szTo, m_szSubject, m_szMessage ))
			return;
	}

	REQUEST_SET Request;
	// get request
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );
	// set variables
	Request.m_library_id = nLibraryID;
	Request.m_status_id = STATUS_ID_NEW;
	// reset owner
	Request.m_staff_id = NULL;
	// set touched
	Request.m_touched = NULL;
	// get unique ID
	Request.m_uid = GetIDString();
	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// set if sticky
	if( appConfig.m_General.bUseStickyNAReason )
		AfxGetApp()->WriteProfileInt( _T("Settings"), _T("StickyNAReason"), m_nReason );

	CDialog::OnOK();
}

void CDlgRequestNA::OnLbnSelchangeListPick()
{
	// update data
	UpdateData();

	// Update buttons
	UpdateButtons();
}

CString CDlgRequestNA::GetIDString(void)
{
	CString szUniqueID;

	// add PMID
	if( !m_szPMID.IsEmpty() )
	{
		szUniqueID.Format( _T("PMID:%s"), m_szPMID );
	}

	// add OCLC
	if( !m_szOCLC.IsEmpty() )
	{
		szUniqueID.Format( _T("OCLC:%s"), m_szOCLC );
	}

	// add NLM ID
	if( !m_szNLMID.IsEmpty() )
	{
		szUniqueID.Format( _T("NLM ID:%s"), m_szNLMID );
	}

	// return
	return szUniqueID;
}

void CDlgRequestNA::UpdateButtons(void)
{
	// get reason ID
	INT nSelection = m_ctrlReason.GetCurSel();
	if( nSelection < 0 )
		return;
	m_nReason = m_aStatusID.GetAt( nSelection );

	// enable send ILL
	if( m_aStatus.GetAt( m_nReason ).nSendILL == 0 )
		GetDlgItem( IDC_BUTTON_ILL )->EnableWindow( FALSE );
	else if( m_nILL > 0 )
		GetDlgItem( IDC_BUTTON_ILL )->EnableWindow();

	// get status description
	STATUS_LOOKUP Status;
	appConfig.m_SQL.GetDescriptionStatus( &Status, m_nReason );
	// show complete
	if( Status.nComplete > 0 )
		SetDlgItemTextW( IDOK, _T("Complete") );
	else if( m_nILL && Status.nSendILL > 0 && !appConfig.m_Options.bDisableCombineNASendReturn )
		SetDlgItemTextW( IDOK, _T("Send to ILL") );
	else
		SetDlgItemTextW( IDOK, _T("Return to user") );
}